package com.blacktea.cloudproviderhystrixpayment.controller;

import com.blacktea.cloudapicommon.beans.CommonResult;
import com.blacktea.cloudproviderhystrixpayment.service.PaymentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: black tea
 * @date: 2021/7/31 13:56
 */
@RestController
@RequestMapping("feign/test")
@Slf4j
@RequiredArgsConstructor
public class FeignTestController {

    private final PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("timeout/{s}")
    public CommonResult timeout(@PathVariable("s") Long time){
        try {
            TimeUnit.SECONDS.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new CommonResult(200,"serverPort:"+serverPort+",超时"+time+"s,成功执行结束!");
    }

    @GetMapping("success/{id}")
    public CommonResult<String> getById(@PathVariable("id") Long id){
        return new CommonResult<>(200,"serverPort:"+serverPort,paymentService.paymentInfoSuccess(id));
    }

    @GetMapping("wait/timeout/{timeout}")
    public CommonResult<String> waitTimeout(@PathVariable("timeout") Long timeout){
        return new CommonResult<>(200,"serverPort:"+serverPort,paymentService.paymentInfoTimeOut(timeout));
    }
}
